﻿using AutoMapper;
using Polly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BasicData.Vendors.DTO;
using WMS.Common;
using WMS.Web.Controllers.BasicData.Vendors;

namespace WMS.BasicData.Vendors
{
    public class VendorAppService: IApplicationService, IVendorAppService
    {

        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IRepository<Vendor, int> _vendorRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        public VendorAppService(IRepository<Vendor, int> vendorRepository)
        {
            _vendorRepository = vendorRepository;
        }

        /// <summary>
        /// 获取供应商（分页）
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">页容量</param>
        /// <param name="chineseName">中文名字</param>
        /// <returns>返回对应数据</returns>
        public async Task<PagedResult> GetVendor(int page, int limit, string? chineseName)
        {
            var list = System.Linq.PredicateBuilder.New<Vendor>(true);
            if (!string.IsNullOrEmpty(chineseName))
            {
                list = list.And(m => m.ChineseName.Contains(chineseName));
            }
            var vendorList = await _vendorRepository.GetListAsync(list!);
            PagedResult pageResult = new PagedResult();
            pageResult.RowCount = page;
            pageResult.PageCount = limit;
            pageResult.CurrentPage = vendorList.Count();
            pageResult.Queryable = vendorList.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return pageResult;

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="vendor">供应商信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddVendor(Vendor vendor)
        {
            var repetition = await _vendorRepository.GetListAsync(m => m.ChineseName == vendor.ChineseName);
            if (repetition.Count()>0)
            {
                return -1;
            }
            await _vendorRepository.InsertAsync(vendor);
            return 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteVendor(int Id)
        {
            await _vendorRepository.DeleteAsync(Id);
            return 1;
        }

        /// <summary>
        /// 获取对应数据
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <returns>返回受影响行数</returns>
        public async Task<Vendor> BackfillData(int Id)
        {
            var vendorList = await _vendorRepository.FindAsync(Id);
            return vendorList;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public async Task<int> UpdateVendor(UpdateVendorDto vendor)
        {
            var existingVendor = await _vendorRepository.GetListAsync(m => m.ChineseName == vendor.ChineseName && m.Id != vendor.Id);
            if (existingVendor.Count()>0)
            {
                return -1;
            }
            var list = vendor.MapTo<Vendor>();

            await _vendorRepository.UpdateAsync(list);
            return 1;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="array">数组</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> BulkDeletion(string array)
        {
           await _vendorRepository.DeleteAsync(m=>array.Contains(m.Id.ToString()));
           return 1;
        }



    }
}
